package com.wei.duowan.bbs.ui.adapter;

import android.content.res.Resources;
import android.support.annotation.IntRange;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 作者：赵若位
 * 时间：2019/7/31 23:27
 * 联系方式：1070138445
 * 功能：
 */
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseViewHolder>
{
    protected static final int EMPTY = 0x100;//无数据
    protected static final int CONTENT = 0x101;//有数据

    protected AppCompatActivity mActivity;
    protected List<T> mList;
    private int mEmptyLayoutID;//空布局
    private int mLayoutID;
    private int mViewCacheSize = 5;
    private boolean isEmpty;//是否显示空布局
    protected Resources mResources;

    public BaseRecyclerViewAdapter(AppCompatActivity activity, @LayoutRes int layoutID)
    {
        if (activity == null)
        {
            throw new NullPointerException("The AppCompatActivity is empty!");
        }
        this.mActivity = activity;
        mResources = App.getResource();
        this.mLayoutID = layoutID;
        this.mEmptyLayoutID = R.layout.layout_empty;
        mList = new ArrayList<>();
    }


    /*RecyclerView绑定Adapter*/
    public void bindRecyclerView(@Nullable RecyclerView recyclerView)
    {
        if (recyclerView == null)
        {
            throw new NullPointerException("The Bind RecyclerView is Empty!");
        }
        /*关闭刷新动效*/
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        recyclerView.setItemViewCacheSize(mViewCacheSize);
        recyclerView.setAdapter(this);
    }


    /*设置空布局ID*/
    public BaseRecyclerViewAdapter setEmpty(@LayoutRes int layoutID)
    {
        this.mEmptyLayoutID = layoutID;
        notifyDataSetChanged();
        return this;
    }


    /*设置ItemView缓存个数*/
    public BaseRecyclerViewAdapter setViewCacheSize(int cacheSize)
    {
        this.mViewCacheSize = cacheSize;
        return this;
    }


    /*更新内容中所有的数据*/
    public void setNewData(@Nullable List<T> list)
    {
        this.mList = (list == null) ? new ArrayList<T>() : list;
        notifyDataSetChanged();
    }

    /*插入一组数据*/
    public void setData(@Nullable List<T> list, @IntRange(from = 0) int position)
    {
        if (list != null && list.size() != 0)
        {
            int index = getInsertPosition(position);
            this.mList.addAll(index, list);
            notifyItemRangeInserted(index, list.size());
        }
    }

    /*插入一组数据*/
    public void setData(@Nullable List<T> list)
    {
        this.setData(list, mList.size());
    }

    /*插入一条数据*/
    public void setData(@Nullable T t, @IntRange(from = 0) int position)
    {
        if (t != null)
        {
            int index = getInsertPosition(position);
            this.mList.add(index, t);
            notifyItemRangeInserted(index, 1);
        }
    }

    /*插入一条数据*/
    public void setData(@Nullable T t)
    {
        this.setData(t, mList.size());
    }


    /*获取数据插入的位置*/
    private int getInsertPosition(@IntRange(from = 0) int position)
    {
        return (position < 0 || position > mList.size()) ? mList.size() : position;
    }

    /*设置是否显示空布局页面*/
    public BaseRecyclerViewAdapter<T> setIsEmpty(Boolean empty)
    {
        this.isEmpty = empty;
        return this;
    }

    /*是否显示空数据Layout*/
    protected final boolean isDisplayEmpty()
    {
        return isEmpty && (mList == null || mList.isEmpty());
    }


    @Override
    public int getItemViewType(int position)
    {
        if (isDisplayEmpty())
        {
            return EMPTY;
        } else
        {
            return CONTENT;
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int type)
    {
        return BaseViewHolder.getViewHolder(mActivity,
                LayoutInflater.from(mActivity.getApplicationContext()).
                        inflate(type == EMPTY ? mEmptyLayoutID : mLayoutID, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position)
    {
        if (getItemViewType(position) == CONTENT && mList != null && mList.get(holder.getAdapterPosition()) != null)
        {
            convert(position, holder, mList.get(holder.getAdapterPosition()));
        }
    }

    @Override
    public int getItemCount()
    {
        return (isDisplayEmpty()) ? 1 : mList.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView)
    {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager)
        {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
            {
                @Override
                public int getSpanSize(int position)
                {
                    return isDisplayEmpty() ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(@NonNull BaseViewHolder holder)
    {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null
                && lp instanceof StaggeredGridLayoutManager.LayoutParams
                && isDisplayEmpty())
        {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(true);
        }
    }

    protected abstract void convert(int position, @Nullable BaseViewHolder holder, @Nullable T t);
}
